package top.byteio.common.core.utils;


import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import lombok.Builder;
import lombok.Getter;

import java.lang.reflect.InvocationTargetException;


@Builder
public class CaptchaGenerate {

    private CaptchaType type;

    private int width;

    private int height;

    public AbstractCaptcha generate() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        return this.type
                .getCaptchaClass()
                .getConstructor(int.class, int.class)
                .newInstance(this.width, this.height);
    }

    public enum CaptchaType {
        // 线段干扰的验证码
        LINE_CAPTCHA(LineCaptcha.class),
        //圆圈干扰验证码
        CIRCLE_CAPTCHA(CircleCaptcha.class),
        //扭曲干扰验证码
        SHEAR_CAPTCHA(ShearCaptcha.class);

        @Getter
        private final Class<? extends AbstractCaptcha> captchaClass;

        CaptchaType(Class<? extends AbstractCaptcha> captchaClass) {
            this.captchaClass = captchaClass;
        }
    }
}
